package com.utils
{
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.mxml.HTTPService;
	import mx.utils.ObjectUtil;
	
	public class BasicService
	{
		private var _url:String;
		protected var service:HTTPService;
		protected var _callBack:Function = null;
		protected var _errorHandler:Function = null;
		
		public function BasicService(url:String = null, method:String = "GET", busyCursor:Boolean = false, callBack:Function=null,errorHandler:Function=null)
		{
			_url = url;
			_callBack = callBack;
			_errorHandler = errorHandler;
			service = new HTTPService();
			service.method = method
			service.showBusyCursor = busyCursor;
			service.resultFormat = "object";
			service.requestTimeout=160;
		}
		
		public function send(paramters:Object = null):void
		{
			service.url = _url;	
			service.addEventListener(ResultEvent.RESULT, resultHandler);
			service.addEventListener(FaultEvent.FAULT, faultHandler);
//			service.addEventListener(HTTPStatusEvent.HTTP_STATUS, statusHandler);
			service.send(paramters);			
		}
		
		public function resultHandler(event:ResultEvent):void
		{
			if(_callBack != null)
				_callBack.call(null,event);
			//trace(ObjectUtil.toString(event.result));
		}
		
		public function faultHandler(event:FaultEvent):void
		{
			if(_errorHandler == null)
				Alert.show(event.message.toString());
			else
				_errorHandler.call(null,event);
		}
	}
}